﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows;
using System.Windows.Navigation;
using System.Windows.Threading;
using EzDesk.Generic;
using EzDesk.Generic.WPF;
using System.Threading;
using EzDesk.MVVMBase;

namespace EzDesk.DesktopClient.PopupModule.View
{
    /// <summary>
    /// Interaction logic for NotificationView.xaml
    /// </summary>
    public partial class PopupView : WindowExtenBase, IPopupView
    {
        private readonly TimeSpan _waitTime = TimeSpan.FromSeconds(8);
        private readonly DispatcherTimer _disappearTimer = new DispatcherTimer();
        public event EventHandler<GenericEventArg<string>> OnNavigate;

        public PopupView()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            InitWindowState();
            InitEvent();
        }
        private void InitWindowState()
        {
            ShowInTaskbar = false;
            PopupHide();
        }
        private void InitEvent()
        {
            InitDispatcherTimer();
            SizeChanged += (o, e) => ResizeWindow();
            this.BodyContent.LayoutUpdated += new EventHandler(BodyContent_LayoutUpdated);
        }

        void BodyContent_LayoutUpdated(object sender, EventArgs e)
        {
            ResizeWindow();
        }


        public void ResizeWindow()
        {
            this.ToBesideScreen(ContentAlignment.TopLeft).ToBeTop();
            this.Height = TitlePanel.ActualHeight + BodyPanel.DesiredSize.Height + 10;
        }

        private void InitDispatcherTimer()
        {
            _disappearTimer.Interval = _waitTime;
            _disappearTimer.Tick +=
                (obj, e) =>
                    IsMouseOver.TrueOrFalseDo_Action(
                        this.PopupShow
                        , this.PopupHide
                    );
        }

        private void CloseBtn_Click(object sender, RoutedEventArgs e)
        {
            //this.MinimizeCurrentWindow();
            this.OnWindowMinimize.ExecuteMethod(sender, e);
            this.PopupHide();
        }

        private void ContentControl_RequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            //OnNavigate.ExecuteMethod(null, "http://localhost:2604/Test.aspx");
            OnNavigate.ExecuteMethod(null, e.Uri.AbsoluteUri);
        }


        public new object ViewModel
        {
            get
            {
                return DataContext;
            }
            set
            {
                DataContext = value;
            }
        }

        public void PopupShow()
        {
            this.Show();
            this.ToBesideScreen(ContentAlignment.TopLeft).ToBeTop();
            this.Visibility = Visibility.Visible;
            this.PreHide();
        }

        private void PreHide()
        {
            ThreadPool.QueueUserWorkItem((state) =>
            {
                this._disappearTimer.Stop();
                Thread.Sleep(_waitTime);
                this._disappearTimer.Start();
            });
        }

        public void PopupHide()
        {
            this.Topmost = false;
            this.Visibility = Visibility.Collapsed;
            this.OnHide.ExecuteMethod(null, null);
        }

        public event EventHandler OnHide;

        public event EventHandler OnWindowMinimize;
    }
}
